package com.nantong.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.nantong.admin.annotation.PermissionRegister;
import com.nantong.admin.domain.SalesOrder;
import com.nantong.admin.domain.model.request.salesorder.SalesOrderSearch;
import com.nantong.admin.domain.model.response.salesorder.SalesOrderVo;
import com.nantong.admin.service.ISalesOrderService;
import com.nantong.common.mybatis.core.page.PageQuery;
import com.nantong.common.mybatis.core.page.TableDataInfo;
import com.nantong.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 销售订单Controller
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/system/sales-order")
@PermissionRegister(
    name = "销售订单管理",
    key = "system:sales-order",
    type = 2,  // 2=菜单
    path = "sales-order",
    component = "/views/SalesOrder/SalesOrderView.vue",
    icon = "shopping",
    sortOrder = 3,
    parentKey = "system"  // 父菜单是system
)
@Tag(name = "销售订单管理", description = "销售订单管理相关接口")
public class SalesOrderController {

    private final ISalesOrderService salesOrderService;

    /**
     * 获取销售订单列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取销售订单列表", description = "获取系统销售订单列表")
    @SaCheckPermission("system:sales-order:list")
    @PermissionRegister(
            name = "销售订单列表查询",
            key = "system:sales-order:list",
            type = 3,  // 3=按钮
            remark = "查询销售订单数据列表"
    )
    public R<List<SalesOrderVo>> list(SalesOrderSearch salesOrderSearch) {
        List<SalesOrderVo> salesOrders = salesOrderService.searchList(salesOrderSearch);
        return R.ok(salesOrders);
    }

    /**
     * 获取销售订单分页
     */
    @SaCheckPermission("system:sales-order:page")
    @GetMapping("/page")
    @Operation(summary = "销售订单分页查询", description = "获取系统销售订单分页数据")
    @PermissionRegister(
            name = "销售订单分页查询",
            key = "system:sales-order:page",
            type = 3,  // 3=按钮
            remark = "查询销售订单数据分页"
    )
    public TableDataInfo<SalesOrderVo> page(SalesOrderSearch salesOrderSearch, PageQuery pageQuery) {
        return salesOrderService.selectPageList(salesOrderSearch, pageQuery);
    }

    /**
     * 获取销售订单详细信息
     */
    @GetMapping("/{salesOrderId}")
    @Operation(summary = "获取销售订单详情", description = "根据销售订单ID获取销售订单详情")
    @SaCheckPermission("system:sales-order:query")
    @PermissionRegister(
            name = "销售订单详情查询",
            key = "system:sales-order:query",
            type = 3,  // 3=按钮
            remark = "查询销售订单数据详情"
    )
    public R<SalesOrderVo> getInfo(@PathVariable Long salesOrderId) {
        SalesOrderVo salesOrder = salesOrderService.selectSalesOrderById(salesOrderId);
        return R.ok(salesOrder);
    }

    /**
     * 新增销售订单
     */
    @PostMapping
    @Operation(summary = "新增销售订单", description = "新增系统销售订单")
    @SaCheckPermission("system:sales-order:add")
    @PermissionRegister(
            name = "销售订单新增",
            key = "system:sales-order:add",
            type = 3,  // 3=按钮
            remark = "新增销售订单数据"
    )
    public R<Void> add(@RequestBody SalesOrder salesOrder) {
        salesOrderService.insertSalesOrder(salesOrder);
        return R.ok();
    }

    /**
     * 修改销售订单
     */
    @PutMapping
    @Operation(summary = "修改销售订单", description = "修改系统销售订单")
    @SaCheckPermission("system:sales-order:edit")
    @PermissionRegister(
            name = "销售订单修改",
            key = "system:sales-order:edit",
            type = 3,  // 3=按钮
            remark = "修改销售订单数据"
    )
    public R<Void> edit(@RequestBody SalesOrder salesOrder) {
        salesOrderService.updateSalesOrder(salesOrder);
        return R.ok();
    }

    /**
     * 删除销售订单
     */
    @DeleteMapping("/{salesOrderId}")
    @Operation(summary = "删除销售订单", description = "删除系统销售订单")
    @SaCheckPermission("system:sales-order:delete")
    @PermissionRegister(
            name = "销售订单删除",
            key = "system:sales-order:delete",
            type = 3,  // 3=按钮
            remark = "删除销售订单数据"
    )
    public R<Void> remove(@PathVariable Long salesOrderId) {
        salesOrderService.deleteSalesOrderById(salesOrderId);
        return R.ok();
    }

}

